﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TarreOA.TarreOAModels;

namespace TarreOA.TarreOADAL
{
    public class PublicCommunicationGrouptbDAO
    {
        /// <summary>
        /// 保存新建的通讯组信息
        /// </summary>
        /// <param name="comGroupName"></param>
        /// <returns></returns>
        public int saveComGroupInfo(string comGroupName)
        {
            int count = 0;
            SqlConnection sqlConn = null;
            SqlCommand sqlCmd = null;
            try
            {
                sqlConn = SQLManager.GetConn();
                sqlConn.Open();
                string sql = "insert into PublicCommunicationGrouptb values( @comGroupName, @createTime)";
                sqlCmd = new SqlCommand(sql, sqlConn);
                sqlCmd.Parameters.Add(new SqlParameter("@comGroupName", comGroupName));
                sqlCmd.Parameters.Add(new SqlParameter("@createTime", DateTime.Now));
                count = sqlCmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sqlConn != null && sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return count;
        }

        /// <summary>
        /// 查找所有公共通讯组信息
        /// </summary>
        /// <returns></returns>
        public List<PublicCommunicationGrouptb> findALL()
        {
            List<PublicCommunicationGrouptb> list = null;
            SqlConnection sqlConn = null;
            SqlCommand sqlCmd = null;
            SqlDataReader sqlDr = null;
            try
            {
                sqlConn = SQLManager.GetConn();
                sqlConn.Open();
                string sql = "select * from PublicCommunicationGrouptb";
                sqlCmd = new SqlCommand(sql, sqlConn);
                sqlDr = sqlCmd.ExecuteReader();
                if (sqlDr.HasRows)
                {
                    list = new List<PublicCommunicationGrouptb>();
                    while (sqlDr.Read())
                    {
                        PublicCommunicationGrouptb pcgtb = new PublicCommunicationGrouptb();
                        pcgtb.PcgId = (int)sqlDr["ComGroupId"];
                        pcgtb.PcgName = (string)sqlDr["ComGroupName"];
                        pcgtb.PcgCreateTime = (DateTime)sqlDr["ComGroupCreateTime"];
                        list.Add(pcgtb);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sqlDr != null)
                {
                    sqlDr.Close();
                    sqlDr.Dispose();
                }
                if (sqlConn != null && sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return list;
        }

        /// <summary>
        /// 根据通讯组编号查找通讯组信息
        /// </summary>
        /// <param name="cgid"></param>
        /// <returns></returns>
        public PublicCommunicationGrouptb findById(int cgid)
        {
            PublicCommunicationGrouptb pcgtb = null;
            SqlConnection sqlConn = null;
            SqlCommand sqlCmd = null;
            SqlDataReader sqlDr = null;
            try
            {
                sqlConn = SQLManager.GetConn();
                sqlConn.Open();
                string sql = "select * from PublicCommunicationGrouptb where ComGroupId = @cgid";
                sqlCmd = new SqlCommand(sql, sqlConn);
                sqlCmd.Parameters.Add(new SqlParameter("@cgid", cgid));
                sqlDr = sqlCmd.ExecuteReader();
                if (sqlDr.HasRows)
                {
                    sqlDr.Read();
                    pcgtb = new PublicCommunicationGrouptb();
                    pcgtb.PcgId = (int)sqlDr["ComGroupId"];
                    pcgtb.PcgName = (string)sqlDr["ComGroupName"];
                    pcgtb.PcgCreateTime = (DateTime)sqlDr["ComGroupCreateTime"];
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sqlDr != null)
                {
                    sqlDr.Close();
                    sqlDr.Dispose();
                }
                if (sqlConn != null && sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return pcgtb;
        }

        /// <summary>
        /// 根据通讯组编号更新通讯组信息
        /// </summary>
        /// <param name="cgid"></param>
        /// <param name="cgName"></param>
        /// <param name="lwtime"></param>
        /// <returns></returns>
        public int updateGroupInfoById(int cgid, string cgName, DateTime lwtime)
        {
            int count = 0;
            SqlConnection sqlConn = null;
            SqlCommand sqlCmd = null;
            try
            {
                sqlConn = SQLManager.GetConn();
                sqlConn.Open();
                string sql = "update PublicCommunicationGrouptb set ComGroupName = @cgName, ComGroupCreateTime = @lwtime where ComGroupId = @cgid";
                sqlCmd = new SqlCommand(sql, sqlConn);
                sqlCmd.Parameters.Add(new SqlParameter("@cgName", cgName));
                sqlCmd.Parameters.Add(new SqlParameter("@lwtime", lwtime));
                sqlCmd.Parameters.Add(new SqlParameter("@cgid", cgid));
                count = sqlCmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sqlConn != null && sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return count;
        }

        /// <summary>
        /// 根据通讯组编号删除通讯组信息
        /// </summary>
        /// <param name="cgid"></param>
        /// <returns></returns>
        public int delById(int cgid)
        {
            int count = 0;
            SqlConnection sqlConn = null;
            SqlCommand sqlCmd = null;
            try
            {
                sqlConn = SQLManager.GetConn();
                sqlConn.Open();
                string sql = "delete from PublicCommunicationGrouptb where ComGroupId = @cgid";
                sqlCmd = new SqlCommand(sql, sqlConn);
                sqlCmd.Parameters.Add(new SqlParameter("@cgid", cgid));
                count = sqlCmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (sqlConn != null && sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                    sqlConn.Dispose();
                }
            }
            return count;
        }

    }
}
